﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class AddOLResults : System.Web.UI.Page
    {
        StudentAL studentAL;

        public Guid SelectedStudent
        {
            get
            {
                if (Session["StudentId"] == null)
                {
                    return studentAL.GetStudents()[0].StudentId;
                }
                else
                {
                    return new Guid(Session["StudentId"].ToString());
                }
            }
            set
            {
                Session["StudentId"] = value;
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

            //List<MemberDTO> memberDTO = memberAL.GetAllMembers();



            /*ddlCommMembrMembrId.DataSource = memberDTO;
            ddlCommMembrMembrId.DataTextField = "FullName";
            ddlCommMembrMembrId.DataValueField = "MemberId";
            ddlCommMembrMembrId.SelectedValue = SelectedMember.ToString();
            ddlCommMembrMembrId.DataBind();*/

            studentAL = new StudentAL();

            List<string> lstOfOLYrs = new List<string>();
            lstOfOLYrs = studentAL.GetStudents().OrderByDescending(b => b.OrdinaryBatchYear).Select(a => a.OrdinaryBatchYear).Distinct().ToList();

            string OLevelYrNotAvailable = "N/A";
            //lstOfOLYrs.Add(OLevelYrNotAvailable);

            //Insert this element at 1sr place
            lstOfOLYrs.Insert(0, OLevelYrNotAvailable);
            ddlOLResultsYr.DataSource = lstOfOLYrs;
            ddlOLResultsYr.DataBind();

            int currentSystemYear = System.DateTime.Now.Year;
            int olresultStartYr = 1950;
            List<string> listOfYrs = new List<string>();
            for (int i = olresultStartYr; i < currentSystemYear ; i++)
            {
                listOfYrs.Add(i.ToString());
            }
            listOfYrs.Add("N/A");
            ddlOLResultsYr.DataSource = listOfYrs;
            ddlOLResultsYr.DataBind();


            //base.OnPreRenderComplete(e);

            List<StudentDTO> studentDTO = new List<StudentDTO>();
           /* if (!ddlOLResultsYr.SelectedValue.Equals("N/A"))
            {
                studentDTO = studentAL.GetStudentByOLYear(ddlOLResultsYr.SelectedValue);
            }
            else
            {
                studentDTO = studentAL.GetStudents();
            }*/

            studentDTO = studentAL.GetStudents();
            ddlOLResultStudentName.DataSource = studentDTO;
            ddlOLResultStudentName.DataTextField = "FullName";
            ddlOLResultStudentName.DataValueField = "StudentId";
            ddlOLResultStudentName.SelectedValue = SelectedStudent.ToString();
            ddlOLResultStudentName.DataBind();



            StudentDTO studentDTOList = studentAL.GetStudentById(new Guid(ddlOLResultStudentName.SelectedValue)).SingleOrDefault();
            if (studentDTOList.IndexNo != "")
            {
                txtOLResultsSchoolIndexNo.Text = studentDTOList.IndexNo;
            }
            else
            {
                txtOLResultsSchoolIndexNo.Text = "";
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
           /* studentAL = new StudentAL();
            if (!IsPostBack)
            {
                bindResults();
            }*/
        }

        protected void btnOLResultCancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnOLResultSubmit_Click(object sender, EventArgs e)
        {

            try
            {
                int year;
                //If year is not knows then enter a value as 9999
                if (ddlOLResultsYr.SelectedValue.Equals("N/A"))
                {
                    year = 9999;
                }
                else
                {
                    year = Convert.ToInt32(ddlOLResultsYr.SelectedValue);
                }
                OLResultsDTO olResultDTO = new OLResultsDTO
                {
                    OLevelResultId = Guid.NewGuid(),
                    StudentId = new Guid(ddlOLResultStudentName.SelectedValue),


                   Year =year,
                    NoOfAs = Convert.ToInt32( ddlOLResultsNoOfAs.SelectedValue),
                    NoOfBs =  Convert.ToInt32(ddlOLResultsNoOfBs.SelectedValue),
                    NoOfCs =  Convert.ToInt32(ddlOLResultsNoOfCs.SelectedValue),
                    NoOfSs =  Convert.ToInt32(ddlOLResultsNoOfSs.SelectedValue),
                    NoOfFs =  Convert.ToInt32(ddlOLResultsNoOfFs.SelectedValue),
                    Notes = txtOLResultSpclNote.Text,
                   //ModifiedBy = "Tharsan",
                    ModifiedTime = DateTime.Now
                };
                int x = Convert.ToInt32(ddlOLResultsNoOfAs.SelectedValue) +
                      Convert.ToInt32(ddlOLResultsNoOfBs.SelectedValue) +
                       Convert.ToInt32(ddlOLResultsNoOfCs.SelectedValue) +
                       Convert.ToInt32(ddlOLResultsNoOfSs.SelectedValue) +
                       Convert.ToInt32(ddlOLResultsNoOfFs.SelectedValue);
                string result = "";
                StudentAL studentAL = new StudentAL();
                if (x <= 12)
                {
                    result = studentAL.saveOLResults(olResultDTO);

                    if (result == "Saved")
                    {
                        lblOLResult.Text = "O/L Results Details added Successfully";
                        //txtCommMembrStartDate.Text = "";
                        //txtCommMembrEnDate.Text = "";
                        //txtCommitteeMemberDescription.Text = "";

                    }
                    else if (result == "Updated")
                    {
                        lblOLResult.Text = "O/L Results Details updated Successfully";
                        //txtCommMembrStartDate.Text = "";
                        //txtCommMembrEnDate.Text = "";
                        //txtCommitteeMemberDescription.Text = "";
                    }
                    else if (result == "AlreadyExists")
                    {
                        lblOLResult.Text = "O/L Results with this student for this year is already exists ";
                    }
                    else
                    {
                        lblOLResult.Text = "Your data cannot be added";
                    }
                }
                else
                {
                    lblOLResult.Text = "Number Of Subjects exceeded the limit";
                }



            }
            catch (Exception ee)
            {
                lblOLResult.Text = "There is an exception occured while inserting the O/L Results details" + ee.ToString();
            }
        }

        protected void ddlOLResultStudentName_SelectedIndexChanged(object sender, EventArgs e)
        {
            studentAL = new StudentAL();
            SelectedStudent = new Guid(ddlOLResultStudentName.SelectedValue);
            SelectedStudent = new Guid(ddlOLResultStudentName.SelectedValue);
            StudentDTO studentDTOList = studentAL.GetStudentById(new Guid(ddlOLResultStudentName.SelectedValue)).SingleOrDefault();
            if (studentDTOList.IndexNo != "")
            {
                txtOLResultsSchoolIndexNo.Text = studentDTOList.IndexNo;
            }
            else
            {
                txtOLResultsSchoolIndexNo.Text = "";
            }
        }

        protected void ddlOLResultsYr_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void bindResults()
        {
            studentAL = new StudentAL();
            int currentSystemYear = System.DateTime.Now.Year;
            int alresultStartYr = 1950;
            List<int> listOfYrs = new List<int>();
            for (int i = alresultStartYr; i < currentSystemYear + 5; i++)
            {
                listOfYrs.Add(i);
            }
            ddlOLResultsYr.DataSource = listOfYrs;
            ddlOLResultsYr.DataBind();


            //base.OnPreRenderComplete(e);

            List<StudentDTO> studentDTO = studentAL.GetStudents();

            ddlOLResultStudentName.DataSource = studentDTO;
            ddlOLResultStudentName.DataTextField = "FullName";
            ddlOLResultStudentName.DataValueField = "StudentId";
            ddlOLResultStudentName.SelectedValue = SelectedStudent.ToString();
            ddlOLResultStudentName.DataBind();



            StudentDTO studentDTOList = studentAL.GetStudentById(new Guid(ddlOLResultStudentName.SelectedValue)).SingleOrDefault();
            if (studentDTOList.IndexNo != "")
            {
                txtOLResultsSchoolIndexNo.Text = studentDTOList.IndexNo;
            }
            else
            {
                txtOLResultsSchoolIndexNo.Text = "";
            }
        }
    }
}